import axios from "axios";
axios.defaults.baseURL = "http://48ea2493.r20.cpolar.top";
const request = (options) => {
  return new Promise((resolve, reject) => {
    axios(options)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
export const getHistory = (data) => {
  return request({
    url: "/gpt/list",
    method: "get",
    params: data,
  });
};
export const chat = (data) => {
  return request({
    url: "/gpt/ask",
    method: "get",
    params: data,
  });
};
const fetchReader = (url, options) => {
  const decoder = new TextDecoder();
  return new Promise((resolve, reject) => {
    fetch(`${url}`, options)
      .then((response) => {
        const reader = response.body?.getReader();
        if (reader) {
          resolve({ reader, decoder });
        } else {
          reject(new Error("Response body is null"));
        }
      })
      .catch((error) => {
        reject(error);
      });
  });
};
export const getFetchSse = (data) => {
  //   const queryString = new URLSearchParams(data).toString(); // 将对象转换为查询字符串
  return fetchReader(`/gpt/stream?question=${data}`, {
    method: "GET",
  });
};
